package com.tnsw.facebookautoliker.helper;

import java.util.Set;

import org.json.JSONArray;
import org.json.JSONObject;

import com.facebook.Session;
import com.tnsw.coreutils.MyLog;
import com.tnsw.coreutils.http.THttpBase;
import com.tnsw.coreutils.http.THttpGet;
import com.tnsw.coreutils.http.THttpListener;
import com.tnsw.facebookautoliker.database.DBManager;
import com.tnsw.facebookautoliker.database.FriendsList;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class TestFBAction2 extends FacebookCommandObject implements THttpListener {

	private String tag = "TestFBAction2";
	private String userId;
	
	public TestFBAction2(Context context, String userId) {
		super(context);
		this.userId = userId;
	}
	
	@Override
	public void execute() {
		super.execute();
		
		Session session = Session.openActiveSessionFromCache(this.context);
		String url = "https://graph.facebook.com/" + this.userId + "/profile?access_token=" + session.getAccessToken() + "&format=json&sdk=android";
		MyLog.d(tag, "using URL=" + url);
		THttpGet h = new THttpGet(context, url);
		h.setListener(this);
		h.addHeader("User-Agent", "FBAndroidSDK.3.0.1");
		h.setSessionName("profile");
		
		h.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "0");
	}

	@Override
	public void onProgressUpdate(THttpBase sender, Integer[] values) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPostExecute(THttpBase sender, Long result) {
		int activeCount = Thread.activeCount();
		Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
		
		MyLog.d(tag, "ThreadCount(activeCount=" + activeCount + ", size=" + threadSet.size() + ")");	
		
		if (sender.getCurrentSB() != null) {
			MyLog.d(tag, "result=" + sender.getCurrentSB().toString());
			try {
				JSONObject json = new JSONObject(sender.getCurrentSB().toString());
				JSONArray jData = json.getJSONArray("data");
				
				String id = "", name = "";
				int i;
				for (i = 0; i < jData.length(); i++) {
					JSONObject jInfo = jData.getJSONObject(0);
					if (jInfo.getString("id") != null) {
						id = jInfo.getString("id");
					}
					
					if (jInfo.getString("name") != null) {
						name = jInfo.getString("name");
					}
				}
				
				MyLog.d(tag, "id=" + id + ", name=" + name);
			
				DBManager dbm = DBManager.getInstance(this.context);
				FriendsList fl = new FriendsList();
				fl.id = id;
				fl.name = name;
				
				dbm.updateFriendsListById(fl);				
			} catch (Exception e) {
				MyLog.e(tag, e.toString(), e);
			}

		}
		
		FacebookBGManager fbm = FacebookBGManager.getInstance(context);
		fbm.updateCommandStatus(this, 200, null);
		MyLog.d(tag, "concurrent=" + fbm.getConcurrent());
		
	}

	@Override
	public void onNewLineFetchBG(THttpBase sender, String line) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onReceiveResponseBG(THttpBase sender) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onBinaryReadBG(THttpBase sender, byte[] b) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void interceptHandler(THttpBase sender) {
		
	}

}
